package com.frxuexi.repair.view

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.RepairBeanItem
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.RequestType
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.repair.R
import com.frxuexi.repair.RepairIntent
import com.frxuexi.repair.RepairViewModel
import com.frxuexi.repair.databinding.ActivityRepairApproveBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.launch

@Route(path = "/repair/RepairApproveActivity")
@AndroidEntryPoint
class RepairApproveActivity : BaseActivity<ActivityRepairApproveBinding,RepairIntent,RepairViewModel>() {
    override val binding: ActivityRepairApproveBinding by lazy { ActivityRepairApproveBinding.inflate(layoutInflater) }
    override val vm: RepairViewModel by viewModels<RepairViewModel>()

    @JvmField
    @Autowired
    var id:Int=-1

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect{
                when(it){
                    is UiState.onSuccess<*> ->{
                        when(it.type){
                            RequestType.UPDATE->{
                                ToastUtils.showLong("审批完成")
                                finish()
                            }
                            else->{
                                val item = it.data as RepairBeanItem
                                binding.bean=item
                                when(item.processingStatus){
                                    "处理中"->{
                                        binding.repairAppCircle2.setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_solid_darkgreen)
                                        binding.repaairGroup.visibility=View.VISIBLE
                                    }
                                    "已完成"->{
                                        binding.repairAppCircle2.setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_solid_darkgreen)
                                        binding.repairAppCircle3.setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_solid_darkgreen)
                                    }else->{
                                    binding.repaairGroup.visibility=View.VISIBLE
                                }
                                }
                            }
                        }

                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        ARouter.getInstance().inject(this)
        vm.sendIntent(RepairIntent.getRepairById(id))

        binding.repairAppChu.setOnClickListener {
            binding.bean?.processingStatus="处理中"
            vm.sendIntent(RepairIntent.updateRepair(id,binding.bean!!))
        }

        binding.repairAppCom.setOnClickListener {
            binding.bean?.processingStatus="已完成"
            vm.sendIntent(RepairIntent.updateRepair(id,binding.bean!!))
        }
    }

}